package com.lkm.app_ui.m.Imple.steelyard_auto;

import android.content.Context;

import com.lkm.app.event.OnNetworkStateEvent;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.GsonHelp;
import com.lkm.app_ui.netapi.AppApi;
import com.lkm.comlib.fun_m.NetworkStateModule;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.market2_0.entity.ResponEntity;



class HoldLoadDeviceinitPoTask extends TaskAdapter<Void, String, ResponEntityAbs<DeviceinitPo>> {

    public static Task<Void, String, ResponEntityAbs<DeviceinitPo>> start() {
        HoldLoadDeviceinitPoTask task = new HoldLoadDeviceinitPoTask(true);
        task.action();
        task.setParam(null);
        task.execTask();
        return task;
    }

    private boolean isHold;

    public HoldLoadDeviceinitPoTask(boolean isHold) {
        super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), HoldLoadDeviceinitPoTask.class.getName(), AppConst.getTaskManager2());
        this.isHold = isHold;
    }

    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);
        if (!getIsContinue()) return;
        mTaskExecutor.executor(() -> {
            requestData();
        });
    }

    private void requestData() {
        if (!getIsContinue()) return;
        String json = AppApi.get_balance_bluetooth(HoldLoadDeviceinitPoTask.this);

        ResponEntity<DeviceinitPo> responEntity = ResponEntity.fromJson(json, DeviceinitPo.class, "");

        if (responEntity.isSuccess()) {
            try {
                String dejson = GsonHelp.toJson(responEntity.getData());
                AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).edit().putString("DeviceinitPo", dejson).commit();
            }catch (Exception e){}

            complete(responEntity);

        } else {
            
            if (isHold && json == null) {
                requestRe();
            } else {
                complete(new ResponEntity().fail(null, responEntity.getMsg()));
            }

        }

    }

    private void requestRe() {
        if (!getIsContinue()) return;
        
        mTaskExecutor.post(() -> {
            doExecuting(getParam());
        }, 5 * 1000);
    }


}
